<?php
  header("Content-type:text/html;charset=utf-8");
  include "Model.php";
  echo "<pre>";
  $conn = new Mysql('school1','client_info');
  $sql  ='show tables';
  $res  = $conn -> getAll($sql); 
  foreach ($res as  $k =>$v){
  	
  	 $info=$v['Tables_in_school1'];
  	 if($info=="client_info"){
  	   $connn =new Mysql('preferredshares','oc_admin_user');
	   $sql = 'select id, user_type,avatar as avatar_img ,province,city,town as district,nickname as nick_name,username as user_name,password as user_password,truename as true_name,sex, mobile,idcard as id_card ,unionid as wx_union_id, openid1 as wx_open_id ,recommender as recommend_user ,recommendedphone as recommend_mobile ,create_time,update_time from  oc_admin_user ;';
	   $res =$connn->getAll($sql);
	   $conn1 =  new Mysql('school1','client_info'); 
		   foreach ($res as $k =>$v){
		        $data =array();
		        $data['id']              = $v['id'];
		        $data['user_type']       = $v['user_type'];
		        $data['avatar_img']      = $v['avatar_img'];
		        $data['province']        = $v['province'];
		        $data['city']            = $v['city'];
		        $data['district']        = $v['district'];
		        $data['nick_name']       = addslashes($v['nick_name']);
		        $data['true_name']       = $v['true_name'];
		        $data['user_name']       = $v['user_name'];
		        $data['user_password']   = $v['user_password'];
		        $data['sex']             = $v['sex'];
		        $data['mobile']          = $v['mobile'];
		        $data['id_card']         = $v['id_card'];
		        $data['wx_union_id']     = $v['wx_union_id'];
		        $data['wx_open_id']      = $v['wx_open_id'];
		        $data['recommend_user']  = $v['recommend_user'];
		        $data['recommend_mobile']= $v['recommend_mobile'];
		        $data['create_time']     = $v['create_time']==0 ? Null:date('Y-m-d H:i:s',$v['create_time']);
		        $data['update_time']     = $v['update_time']==0 ? Null:date('Y-m-d H:i:s',$v['update_time']);
		        //echo '<pre>';
		        //var_dump($data);
		        //exit;
                $res=$conn1->insert($data);
		} 
  	 }elseif($info =="client_initiator_company"){
	 	    $conn =new Mysql('preferredshares','oc_user_org');
            $sql ='select type ,uid as client_id ,equity_time as confirm_time, equity,createtime as create_time ,parent_id as parent_cid, m.id as management_id 
		    from  oc_user_org as o 
		    left join client_management as m on  uid =m.client_id  
		    having type=3';
		    $res=$conn->getAll($sql);
            //var_dump($res);
            $conn1 =  new Mysql('school1','client_initiator_company'); 
	        foreach ($res as $k =>$v){
		        $data =array();
		        $data['client_id']       =$v['client_id'];
		        $data['confirm_time']    =$v['confirm_time']==Null? Null:date('Y-m-d H:i:s',$v['confirm_time']);
		        $data['create_time']     =$v['create_time']==0 ? Null:date('Y-m-d H:i:s',$v['create_time']);
		        $data['parent_cid']      =$v['parent_cid'];
		        $data['management_id']   =$v['management_id'];
		        $data['is_management']   =isset($v['management_id'])?1:0;
		        $data['update_time']     ='null';
		        $data['is_qualified']    =$v['equity']==Null ? 0 : 1;
		        //echo '<pre>';
		        //var_dump($data);
		        //exit;
                $res=$conn1->insert($data);
		} 
  	 }elseif($info == 'client_partner'){

            $conn =new Mysql('preferredshares','oc_user_org');
       
	        $sql  = 'select type, uid as client_id , equity , createtime as create_time ,equity_time as confirm_time,ic.id as init_comp_id, cm.id as management_id 
	        from oc_user_org as o 
	        left join client_initiator_company as ic on uid = ic.client_id 
	        left join client_management        as cm on o.uid = cm.client_id 
	        having type=4;'; 
	        $res =$conn->getAll($sql);
	        //var_dump($res);
	        $conn1 =  new Mysql('school1','client_partner'); 
	        foreach ($res as $k =>$v){
		        $data =array();
		        $data['client_id']       =$v['client_id'];
		        $data['confirm_time']    =$v['confirm_time'];
		        $data['create_time']     =$v['create_time']==0 ? Null:date('Y-m-d H:i:s',$v['create_time']);
		        $data['init_comp_id']    =$v['init_comp_id'];
		        $data['management_id']   =$v['management_id'];
		        $data['is_management']   =isset($v['management_id'])?1:0;
		        $data['update_time']     =Null;
		        $data['is_qualified']    =$v['equity']==Null ? 0 : 1;
		        //var_dump($data);
		        //exit;
                $res=$conn1->insert($data);
		} 
  	 }elseif($info =='client_financial_planner'){

            $conn =new Mysql('preferredshares','oc_user_org');
            $sql  = 'select type, uid as client_id , equity , certificate, createtime as create_time ,equity_time as  confirm_time,ic.id as init_comp_id, cm.id as management_id ,cp.id as partner_id
            from oc_user_org as o 
	        left join client_initiator_company as ic on o.uid = ic.client_id 
	        left join client_management        as cm on o.uid = cm.client_id
		    left join client_partner           as cp on o.uid = cp.client_id 
	        having type=5'; 
	        $res =$conn->getAll($sql);
	        //var_dump($res);
	        //exit;
	        $conn1 =  new Mysql('school1','client_financial_planner'); 
	        foreach ($res as $k =>$v){
		        $data =array();
		        $data['client_id'] =$v['client_id'];
                $data['is_planner'] =$v['certificate']==Null ? 0 : 1;
                $data['is_qualified'] =$v['equity']==Null ? 0 : 1;
                $data['create_time']  =$v['create_time']==0 ? Null:date('Y-m-d H:i:s',$v['create_time']);
                $data['confirm_time']  =$v['confirm_time']==Null ? Null:date('Y-m-d H:i:s',$v['confirm_time']);
                $data['partner_id']   =$v['partner_id'];
                $data['init_comp_id'] = $v['init_comp_id'];
                $data['management_id'] = $v['management_id'];
                $data['update_time']     =Null; 
                $res=$conn1->insert($data);
		        // var_dump($data);
		        //exit;
                //$res=$conn1->insert($data);
		} 
	 }elseif($info =='client_investor'){
            $conn =new Mysql('preferredshares','oc_user_org');
	        $sql  = 'select type, uid as client_id , equity , certificate, createtime as create_time ,equity_time as confirm_time,ic.id as init_comp_id, cm.id as management_id ,cp.id as partner_id,
	        cf.id as planner_id from oc_user_org as o 
	        left join client_financial_planner as cf on o.uid = cf.client_id
	        left join client_initiator_company as ic on o.uid = ic.client_id 
	        left join client_management        as cm on o.uid = cm.client_id
		    left join client_partner           as cp on o.uid = cp.client_id 
	        having type=6 ';
	        $res =$conn->getAll($sql);
	        //var_dump($res);
	        //exit;
	        $conn1 =  new Mysql('school1','client_investor'); 
	        foreach ($res as $k =>$v){
		        $data =array();
		        $data['client_id'] =$v['client_id'];
                $data['is_investor'] =$v['certificate']==Null ? 0 : 1;
                $data['is_qualified'] =$v['equity']==Null ? 0 : 1;
                $data['create_time']  =$v['create_time']==0 ? Null:date('Y-m-d H:i:s',$v['create_time']);
                $data['confirm_time']  =$v['confirm_time']==Null ? Null:date('Y-m-d H:i:s',$v['confirm_time']);
                $data['partner_id']   =$v['partner_id'];
                $data['planner_id']   =$v['planner_id'];
                $data['init_comp_id'] = $v['init_comp_id'];
                $data['management_id'] = $v['management_id'];
                $data['update_time']     =Null; 
                //$res=$conn1->insert($data);
		        //var_dump($data);
		        //exit;
                $res=$conn1->insert($data);
		} 
	 }
  }